package android.src.main.java.com.baidu.mapapi.base;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.VersionInfo;
import com.baidu.mapapi.common.BaiduMapSDKException;

/** FlutterBmfbasePlugin */
public class FlutterBmfbasePlugin implements FlutterPlugin, MethodCallHandler {
  private MethodChannel channel;
  private static final String CHANNEL_NAME = "flutter_bmfbase";

  // Method Names
  private static final String METHOD_SET_API_KEY = "flutter_bmfbase/sdk/setApiKey";
  private static final String METHOD_GET_NATIVE_SDK_VERSION = "flutter_bmfbase/sdk/getNativeBaseVersion";
  private static final String METHOD_SET_PRIVACY_API_KEY = "flutter_bmfbase/sdk/setAgreePrivacy";
  private static final String METHOD_GET_ANDROID_VERSION = "flutter_bmfbase/sdk/getAndroidSdkVersion";

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
    channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    if (channel != null) {
      channel.setMethodCallHandler(null);
      channel = null;
    }
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    try {
      switch (call.method) {
        case METHOD_GET_NATIVE_SDK_VERSION:
          handleGetNativeVersion(result);
          break;
        case METHOD_SET_API_KEY:
          handleSetApiKey(call, result);
          break;
        case METHOD_SET_PRIVACY_API_KEY:
          handleSetPrivacy(call, result);
          break;
        case METHOD_GET_ANDROID_VERSION:
          result.success(android.os.Build.VERSION.SDK_INT);
          break;
        default:
          result.notImplemented();
      }
    } catch (Exception e) {
      result.error("UNKNOWN_ERROR", e.getMessage(), null);
    }
  }

  private void handleGetNativeVersion(Result result) {
    Map<String, String> versionMap = new HashMap<>();
    versionMap.put("version", VersionInfo.getApiVersion());
    versionMap.put("platform", "Android");
    result.success(versionMap);
  }

  private void handleSetApiKey(MethodCall call, Result result) {
    if (call.hasArgument("BMF_COORD_TYPE")) {
      int nCoordType = call.argument("BMF_COORD_TYPE");
      nCoordType = nCoordType - 1; // Flutter -> Android 枚举值转换

      if (nCoordType >= 0 && nCoordType < CoordType.values().length) {
        SDKInitializer.setCoordType(CoordType.values()[nCoordType]);
        result.success(null);
      } else {
        result.error("INVALID_COORD_TYPE", "Invalid coordinate type value", null);
      }
    } else {
      result.error("MISSING_ARGUMENT", "BMF_COORD_TYPE is required", null);
    }
  }

  private void handleSetPrivacy(MethodCall call, Result result) {
    if (call.hasArgument("isAgree")) {
      boolean isAgree = call.argument("isAgree");
      if (BmfMapApplication.mContext != null) {
        try {
          SDKInitializer.setAgreePrivacy(BmfMapApplication.mContext, isAgree);
          result.success(null);
        } catch (BaiduMapSDKException e) {
          result.error("PRIVACY_ERROR", e.getMessage(), null);
        }
      } else {
        result.error("CONTEXT_MISSING", "Application context is not available", null);
      }
    } else {
      result.error("MISSING_ARGUMENT", "isAgree is required", null);
    }
  }
}